﻿using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Servsers
{
    public class NotifyServers
    {
        private readonly IRepository<notify> notifyRepository;

        public NotifyServers()
        {
            notifyRepository = new DapperRepository<notify>();

        }

        public List<notify> GetData(string user_id, string begin_date, string end_date, string type_id, string subject
                   )
        {
            var datas = notifyRepository.GetAllEnumerable();
            if (!string.IsNullOrEmpty(user_id))
            {
                datas = datas.Where(m => m.USER_ID == user_id);
            }
            if (!string.IsNullOrEmpty(begin_date))
            {
                datas = datas.Where(m => m.BEGIN_DATE == DateTime.Parse(begin_date));
            }
            if (!string.IsNullOrEmpty(end_date))
            {
                datas = datas.Where(m => m.END_DATE == DateTime.Parse(end_date));
            }
            if (!string.IsNullOrEmpty(type_id))
            {
                datas = datas.Where(m => m.TYPE_ID == type_id);
            }
            if (!string.IsNullOrEmpty(subject))
            {
                datas = datas.Where(m => m.SUBJECT == subject);
            }

            return datas.ToList();
        }

        public notify GetModelById(string id)
        {
            return notifyRepository.GetById(id);
        }


        internal void Add(notify model)
        {
            notifyRepository.Insert(model);
        }

        internal void Update(notify model)
        {
            notifyRepository.Update(model);
        }

        internal void Del(string id)
        {
            notifyRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetTYPE_ID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = notifyRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 142");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }
    }
}